<template>
  <a-spin :spinning="loading">
    <div class="book-container">

      <!-- 详情 -->
      <div class="tjbox">
        <div class="tjimg"><img :src="coverUrl"/></div>
        <div class="tjinfo">
          <div class="title">
            <h1>{{ query.title }}</h1>
          </div>
          <p>作者：{{ query.author }}</p>
          <p>ISBN：{{ query.isbn }}</p>
          <p>出版社：{{ query.publisher }}</p>
        </div>
      </div>

      <!-- 馆藏 -->
      <div v-if="holding.length > 0" class="guancangbox">
        <h1>馆藏信息</h1>
        <div class="guancangtable">
          <table>
            <tr>
              <th>索书号</th>
              <th>状态</th>
              <th>所属馆</th>
              <th>所在馆</th>
            </tr>
            <tr v-for="(item, index) in holding" :key="index" :class="{inguan: item.state}">
              <td>{{ item.barcode }}</td>
              <td>{{ item.state == 1 ? '在馆' : '外借' }}</td>
              <td>{{ item.orglibName }}</td>
              <td>{{ item.curlibName }}</td>
            </tr>
          </table>
        </div>
      </div>

      <div class="btn-return">
        <div @click="backIndex">返回</div>
      </div>

    </div>
  </a-spin>
</template>
<script>
// 图书详情
import { dataApi } from '@/api/api'
export default {
  name: 'Book',
  data () {
    return {
      loading: false,
      query: {},
      coverUrl: '',
      holding: []
    }
  },
  mounted () {
    this.query = this.$route.query
    this.getInfo()
    this.getCover()
  },
  methods: {
    getInfo () {
      this.loading = true
      const params = {
        key: this.query.bookrecno
      }
      dataApi.bookInfo(params)
        .then(res => {
          if (res.data.code === 200) {
            const resData = res.data.data
            this.holding = resData.holding
          } else {
            this.$message.error(res.data.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    getCover () {
      this.$axios.get(`https://book-resource.dataesb.com/websearch/metares?glc=P1HNQ0898005&cmdACT=getImages&type=0&isbns=${this.query.isbn}`)
        .then(res => {
          var data = JSON.parse(res.data.substring(1, res.data.length - 1))
          if (data.result.length) {
            this.coverUrl = data.result[0].coverlink
          } else {
            this.coverUrl = require('@/assets/images/default-book-image.jpg')
          }
        })
    },
    backIndex () {
      this.$router.push('/recommend')
    }
  }
}
</script>
<style lang="scss" scoped>
.tjbox {
  width: 100%;
  min-height: 3.4rem;
  display: flex;
  align-items: flex-start;
  background: url("~@/assets/images/recommend/bookbg.jpg") no-repeat #eaeaea;
  background-size: 100% 100%;
}
.tjimg {
  box-shadow: -0.08rem 0.08rem 0 rgba(14, 4, 7, 0.25);
  font-size: 0;
  margin: .64rem .5rem 0 0.22rem;
  img {
    width: 1.54rem;
    height: 2.2rem;
  }
}
.tjinfo {
  width: calc(100% - 2.26rem);
  padding: .64rem .32rem 0.2rem 0;
}
.tjbox .title {
  height: .94rem;
  display: flex;
  align-items: center;
  margin-bottom: 0.18rem;
}
.tjinfo h1 {
  font-size: .34rem;
  line-height: .47rem;
  color: #a84a0b;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.tjinfo p {
  font-size: 0.25rem;
  line-height: .38rem;
  color: #be4c00;
}
.guancangbox {
  font-size: 0.22rem;
}
.guancangbox h1 {
  font-size: 0.28rem;
  color: #000;
  padding-left: .7rem;
  background: url("~@/assets/images/recommend/icon1.png") no-repeat 0.2rem center;
  background-size: .32rem .32rem;
  margin: .32rem 0;
}
.guancangtable {
  width: calc(100% - .4rem);
  margin: 0 0.2rem;
  border-radius: 5px;
  overflow: hidden;
  border: 1px solid #d8d8d8;
}
.guancangbox table {
  width: 100%;
}
.guancangbox tr {
  text-align: center;
  line-height: .64rem;
  border-bottom: 1px solid #d8d8d8;
}
.guancangbox tr:last-child {
  border-bottom: none;
}
.guancangbox tr th {
  color: #5e7398;
  font-size: 0.25rem;
  font-weight: normal;
}
.guancangbox tr:nth-child(2n - 1) {
  background: #f5f8fd;
}
.guancangbox tr:nth-child(2n) {
  background: #FFFFFF;
}
.guancangbox tr.inguan {
  color: #005aff;
}
.btn-return {
  width: calc(100% - .4rem);
  padding-top: .32rem;
  margin: 0 0.2rem .32rem;
  display: flex;
  justify-content: flex-end;
}
.btn-return div {
  color: #fff;
  background: #ff9000;
  padding: 0.12rem .32rem;
  font-size: 0.25rem;
  border-radius: 4px;
}
</style>
